package com.fengniu.ejb.business.impl;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.business.ITagLogBusinessLocal;
import com.fengniu.ejb.business.ITagLogBusinessRemote;
import com.fengniu.ejb.dao.CategoryDaoLocal;
import com.fengniu.ejb.dao.ContentDaoLocal;
import com.fengniu.ejb.dao.DeltaDaoLocal;
import com.fengniu.ejb.dao.SiteDaoLocal;
import com.fengniu.ejb.dao.TagLogDaoLocal;
import com.fengniu.ejb.dao.UserDaoLocal;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Content2Content;
import com.fengniu.ejb.entity.TagLog;
import com.fengniu.ejb.entity.TagLogLink;
import com.tianbing.adlottery.global.IConstants;

@Stateless
@LocalBinding(jndiBinding = IEJBConstants.TagLogBusinessLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.TagLogBusinessRemoteJndiName)
public class TagLogBusinessImpl implements ITagLogBusinessLocal, ITagLogBusinessRemote {

	@EJB(name = IEJBConstants.UserDaoLocalJndiName)
	private UserDaoLocal userDao;

	@EJB(name = IEJBConstants.SiteDaoLocalJndiName)
	private SiteDaoLocal siteDao;

	@EJB(name = IEJBConstants.ContentDaoLocalJndiName)
	private ContentDaoLocal contentDao;

	@EJB(name = IEJBConstants.CategoryDaoLocalJndiName)
	private CategoryDaoLocal categoryDao;

	@EJB(name = IEJBConstants.TagLogDaoLocalJndiName)
	private TagLogDaoLocal tagLogDao;

	@EJB(name = IEJBConstants.DeltaDaoLocalJndiName)
	private DeltaDaoLocal deltaUCSGRelationDao;

	public void exportTagLogs(int pgno, int pgsize) {
		//0. get list of TagLogs to be imported
		Integer[] relationIds = { IConstants.RelationID.C2C_IS_REFER_LINK_OF.getId() };
		Integer[] statusIds = { IConstants.Status.IS_EDITING.getId() };
		List<TagLog> newTagLogs = tagLogDao.getListTagLog(statusIds, pgno, pgsize, null);
		if (newTagLogs != null && newTagLogs.size() > 0) {
			for (int i = 0; i < newTagLogs.size(); i++) {
				TagLog tagLog = newTagLogs.get(i);

				List<TagLogLink> targetUrls = tagLog.getTagLogLinks();
				TagLogLink srcTL = new TagLogLink();
				srcTL.setUrl(tagLog.getSrcUrl());
				srcTL.setTitle(tagLog.getSrcTitle());
				targetUrls.add(srcTL);

				//1. import:-create Content from TagLog's srcUrl,targetUrls
				if (targetUrls != null && targetUrls.size() > 0) {
					for (int j = 0; j < targetUrls.size(); j++) {
						String contentId = getComputedContentIdByUrl(targetUrls.get(j).getUrl());
						Content c = contentDao.findContentByContentId(contentId);
						if (c == null) {
							c = new Content();
							c.setName(targetUrls.get(j).getTitle());
							c.setContentId(contentId);
							contentDao.saveContent(c);
						}
					}
				}
				//2. import:-create Content2Content from srcUrl to targetUrl
				String srcContentId = getComputedContentIdByUrl(tagLog.getSrcUrl());
				if (srcContentId != null) {
					targetUrls = tagLog.getTagLogLinks();;
					if (targetUrls != null && targetUrls.size() > 0) {
						for (int j = 0; j < targetUrls.size(); j++) {
							String contentId2 = getComputedContentIdByUrl(targetUrls.get(j).getUrl());
							Content content = contentDao.findContentByContentId(srcContentId);
							Content content2 = contentDao.findContentByContentId(contentId2);
							if (content != null && content2 != null) {
								List<Content2Content> c2cs = contentDao.getListContent2ContentBy(srcContentId,
										relationIds, null, contentId2, 0, 1, null);
								Content2Content c2c;
								if (c2cs != null && c2cs.size() > 0) {
									//already exist,increase count of refer links
									c2c = c2cs.get(0);
									c2c.setRank(c2c.getRank() + 1);
								} else {
									c2c = new Content2Content();
									c2c.setContent(content);
									c2c.setRelationId(IConstants.RelationID.C2C_IS_REFER_LINK_OF.getId());
									c2c.setContent2(content2);
									c2c.setRank(0);
								}
								contentDao.saveContent2Content(c2c);
							}
						}
					}
				}
				//done export taglog,update taglog status
				tagLog.setStatus(IConstants.Status.IS_APPROVE_SUCC.getId());
				tagLogDao.saveTagLog(tagLog);
			}
		}
	}


	public List<Content2Content> getListRelatedUrls(String srcUrl, int pgno, int pgsize) {
		String srcContentId = getComputedContentIdByUrl(srcUrl);
		Integer[] relationIds = { IConstants.RelationID.C2C_IS_REFER_LINK_OF.getId() };
		//here may be conplicated business rules
		List<Content2Content> relatedUrls = contentDao.getListContent2ContentBy(srcContentId, relationIds, null, null,
				pgno, pgsize, null);
		//if not found
		if (relatedUrls == null || relatedUrls.size() == 0) {
			srcContentId = getDefaultByUrl(srcUrl);
		}
		return relatedUrls;
	}

	/**
	 * 
	 * @param srcUrl
	 * @return
	 */
	private String getComputedContentIdByUrl(String srcUrl) {
		return srcUrl; //sample
	}

	private String getDefaultByUrl(String srcUrl) {
		return "www.hongguozi.com"; //e.g. the domain of srcUrl
	}

}
